import {
  put, takeLatest, all, select,
} from 'redux-saga/effects'
import { projectLabelView as projectDuck } from 'ducks'

function* setActiveClassSaga(action) {
  const classes = yield select(projectDuck.selectors.classes)
  yield put(projectDuck.creators.actionSelectBrush(classes[action.value].type))
  const editLabelId = yield select(projectDuck.selectors.editLabelId)
  if (editLabelId !== undefined) {
    yield put(
      projectDuck.creators.actionDoAndUpdate(
        editLabelId,
        projectDuck.creators.actionChangeLabelClass,
        action.value,
      ),
    )
  }
}

export default function* sagas() {
  yield all([takeLatest(projectDuck.types.ACTIVE_LABEL, setActiveClassSaga)])
}
